probe: fix J-Link swo_read crash when swo_num_bytes() returns a negative value - #2021
Open
adityaanikam wants to merge 2 commits into
Open
probe: fix J-Link swo_read crash when swo_num_bytes() returns a negative value#2021adityaanikam wants to merge 2 commits into
adityaanikam wants to merge 2 commits into
Conversation
…ive value Signed-off-by: adityaanikam <adityanikam9502@gmail.com>
Collaborator
|
Actually self._link.swo_num_bytes() does not retrun a negative value (already caught in pylink with error raised). It can however return 0 which is normal when there is no data. When 0 is passed to pylink swo_read() it crashes with IndexError. The required guard should only check for zero value. Please update the code and commit message. |
| try: | ||
| return self._link.swo_read(0, self._link.swo_num_bytes(), True) | ||
| count = self._link.swo_num_bytes() | ||
| if count <= 0: |
Collaborator
There was a problem hiding this comment.
Suggested change
| if count <= 0: | |
| if count == 0: |
RobertRostohar
requested changes
Sep 3, 2026
JLinkProbe.swo_read() passed self._link.swo_num_bytes() straight into swo_read() as the count. pylink allocates a ctypes buffer sized to that count; passing 0 (normal when there is no SWO data available) crashes with IndexError, which isn't a JLinkException and so isn't caught by the existing except clause -- it kills the SWV reader thread outright. swo_num_bytes() itself never returns a negative value (pylink validates and raises internally), so the guard only needs to cover zero. Return an empty bytearray() in that case, matching DebugProbe.swo_read's documented return type. Fixes pyocd#2019.
Author
|
Good catch, thanks , you're right that swo_num_bytes() can't go negative since pylink validates that internally. Updated the guard to |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #2019.
JLinkProbe.swo_read() passed self._link.swo_num_bytes() straight into swo_read() as the count.
pylink allocates a ctypes buffer sized to that count, so passing 0 -- which is normal when no SWO
data is available -- crashes with IndexError, which isn't a JLinkException and so isn't caught by
the existing except clause either; it kills the SWV reader thread outright.
self._link.swo_num_bytes() never returns a negative value (pylink validates and raises internally
if the underlying call fails), so the guard only needs to check for zero, not negative -- thanks to
@RobertRostohar for catching that in review.
Guards the count before the call and returns an empty bytearray() when it's zero, matching
DebugProbe.swo_read's documented return type.
No test added, JLinkProbe's constructor calls into the real J-Link DLL and raises ProbeError if it's
not present, so testing this cleanly would mean bypassing normal construction in a way nothing else
in this codebase does.